//
//  CharBuffRes.m
//  CandyJump
//
//  Created by Bon Lam on 02/07/2011.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "CharBuffRes.h"
#import	"CharacterBuff.h"

#import "PRFile.h"

#define CHAR_BUFF_RES_FILE_XML						@"StaticData/GameData/CharBuffRes.xml"

#define CHAR_BUFF_RES_XML_ELEM_TAG_CHARBUFF			@"charBuff"
#define CHAR_BUFF_RES_XML_ELEM_TAG_SETTINGS			@"settings"
#define CHAR_BUFF_RES_XML_ELEM_TAG_PARAM			@"buffParam"

#define CHAR_BUFF_RES_XML_ATTR_TAG_CLASS			@"class"
#define CHAR_BUFF_RES_XML_ATTR_TAG_CODE				@"code"
#define CHAR_BUFF_RES_XML_ATTR_TAG_TYPE				@"type"
#define CHAR_BUFF_RES_XML_ATTR_TAG_POWERBOOST		@"powerBoost"

//=====================================================================================================================
@implementation CharBuffRes

@synthesize characterBuffInfoDict;

static CharBuffRes *sharedRes = nil;

#pragma mark -
#pragma mark Singleton Object Functions
//---------------------------------------------------------------------------------------------------------------------
- (id)init
{
	self = [super init];
	if (self == nil)  {return self;}
	
	return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (void)dealloc
{	
	[characterBuffInfoDict release];
	characterBuffInfoDict = nil;
	
	[super dealloc];
}

//---------------------------------------------------------------------------------------------------------------------
+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedRes] retain];
}

//---------------------------------------------------------------------------------------------------------------------
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (id)retain 
{
    return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (NSUInteger)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

//---------------------------------------------------------------------------------------------------------------------
- (void)release
{
    //do nothing
}

//---------------------------------------------------------------------------------------------------------------------
- (id)autorelease
{
    return self;
}

//---------------------------------------------------------------------------------------------------------------------
+ (CharBuffRes *)sharedRes
{
    if (sharedRes == nil) {
        sharedRes = [[super allocWithZone:NULL] init];
    }
    return sharedRes;
}

#pragma mark CharBuffRes - Load Data
//---------------------------------------------------------------------------------------------------------------------
- (void)loadCharBuff
{
	if (characterBuffInfoDict != nil) {
		[characterBuffInfoDict release];
		characterBuffInfoDict = nil;
	}
	characterBuffInfoDict = [[NSMutableDictionary alloc] init];
	
	NSString* filePath = [PRFile getFilePath:CHAR_BUFF_RES_FILE_XML];
	[self parseXMLFile:filePath];
}

#pragma mark CharBuffRes - NSXMLParserDelegate
//---------------------------------------------------------------------------------------------------------------------
- (BOOL)parseXMLFile:(NSString *)filename
{
	//  Load Background XML file
	NSData *xmlData = [NSData dataWithContentsOfFile:filename];
	
	NSAssert(xmlData != nil, @"[CharBuffRes parseXMLFile:filename] Error - Unable to load file data: ""%@""", filename);
	
	NSError* error = nil;
	[self parseXMLData:xmlData parseError:&error];
	
	return (error == nil);
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parseXMLData:(NSData *)data parseError:(NSError **)error
{
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
	
    NSError *parseError = [parser parserError];
    if (parseError && error) {*error = parseError;}
    [parser release];
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if ([elementName isEqualToString:CHAR_BUFF_RES_XML_ELEM_TAG_CHARBUFF])
	{
		NSString *className = [attributeDict valueForKey:CHAR_BUFF_RES_XML_ATTR_TAG_CLASS];
		Class modelClass = NSClassFromString(className);
		if(modelClass == nil) { 
			NSLog(@"[CharBuffRes parser: didStartElement: namespaceURI: qualifiedName: attributes:] Error - Unable to find class: ""%@"" ", className);
			return;
		}
		
		NSString *code = [attributeDict valueForKey:CHAR_BUFF_RES_XML_ATTR_TAG_CODE];
		if(code == nil) {
			NSLog(@"[CharBuffRes parser: didStartElement: namespaceURI: qualifiedName: attributes:] Error - Unable to read buff code");
			return;
		}
		
		NSString *type = [attributeDict valueForKey:CHAR_BUFF_RES_XML_ATTR_TAG_TYPE];
		if(type == nil) {
			NSLog(@"[CharBuffRes parser: didStartElement: namespaceURI: qualifiedName: attributes:] Error - Unable to read buff type");
			return;
		}
		
		currentReadingObject = [[modelClass alloc] init];
		currentReadingObject.charBuffType = [CharacterBuffInfo charBuffTypeByString:type];
		[characterBuffInfoDict setObject:currentReadingObject forKey:code];
		
		return;
	}
	
	if ([elementName isEqualToString:CHAR_BUFF_RES_XML_ELEM_TAG_SETTINGS])
	{
		if (currentReadingObject == nil) {return;}
		
		for (NSString* key in [attributeDict allKeys]) {
			id value = [attributeDict objectForKey:key];
			[currentReadingObject setValue:value forKey:key];
		}
		
		return;
	}
	
	
	if ([elementName isEqualToString:CHAR_BUFF_RES_XML_ELEM_TAG_PARAM])
	{
		if (currentReadingObject == nil) {return;}
		
		[currentReadingObject.paramDictionary setValuesForKeysWithDictionary:attributeDict];
		
		return;
	}
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	if ([elementName isEqualToString:CHAR_BUFF_RES_XML_ELEM_TAG_CHARBUFF]) {
		[currentReadingObject release];
		currentReadingObject = nil;
	}
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
	
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	
}

@end
